<?php
/**
*
* @package ppkBB3cker
* @version $Id: acp_board_add1_tzones.php 1.000 2009-08-13 12:03:00 PPK $
* @copyright (c) 2009 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

$user->add_lang('mods/acp/ppkbb3cker_tracker');
$zones_title='ACP_TRACKER_ZONES';
if(request_var('add_zones', 0))
{
	$d_zones=array();
	$updated=0;
	foreach(@$_POST['z_name'] as $k=>$v)
	{
		$zone_type=(@$_POST['z_type'][$k]=='w' || @$_POST['z_type'][$k]=='b' || @$_POST['z_type'][$k]=='r') ? @$_POST['z_type'][$k] : 'a';
		if(@$_POST['z_delete'][$k])
		{
			$d_zones[]=my_int_val($k);
		}
		else if ($k==0 && @$_POST['z_name'][$k]!='')
		{
			$sql='INSERT INTO '.TRACKER_ZONES_NAME." (zone_name, zone_type) VALUES('".$db->sql_escape($_POST['z_name'][$k])."', '{$zone_type}')";
			$result=$db->sql_query($sql);
		}
		else if(@$_POST['z_name'][$k]!='')
		{
			$sql='UPDATE '.TRACKER_ZONES_NAME." SET zone_name='".$db->sql_escape($_POST['z_name'][$k])."', zone_type='{$zone_type}' WHERE id='".my_int_val($k)."'";
			$result=$db->sql_query($sql);
			$updated=1;
		}
	}

	if($d_zones)
	{

		$sql='DELETE FROM '.TRACKER_ZONES_NAME." WHERE id IN('".implode("', '", $d_zones)."')";
		$result=$db->sql_query($sql);

		$sql='DELETE FROM '.TRACKER_ZONES_ADDR." WHERE zone_id IN('".implode("', '", $d_zones)."')";
		$result=$db->sql_query($sql);

		$sql='DELETE FROM '.TRACKER_RTRACK_TABLE." WHERE zone_id IN('".implode("', '", $d_zones)."')";
		$result=$db->sql_query($sql);
	}
}
if(request_var('add_addr', 0) && ($tzone=request_var('tzone', 0)))
{
	$d_addr=array();
	foreach(@$_POST['addr_start'] as $k=>$v)
	{
		if(@$_POST['addr_delete'][$k])
		{
			$d_addr[]=my_int_val($k);
		}
		else if ($k==0 && @$_POST['addr_start'][$k]!='' && @$_POST['addr_end'][$k]!='')
		{
			$addr_start=check_ip(@$_POST['addr_start'][$k]);
			$addr_end=check_ip(@$_POST['addr_end'][$k]);
			if($addr_start && $addr_end)
			{
				$sql='INSERT INTO '.TRACKER_ZONES_ADDR."
					(zone_id, addr_start, addr_end)
						VALUES('{$tzone}', INET_ATON('{$addr_start}'), INET_ATON('{$addr_end}'))";
				$result=$db->sql_query($sql);
			}
		}
		else if(@$_POST['addr_start'][$k]!='' && @$_POST['addr_end'][$k]!='')
		{
			$addr_start=check_ip(@$_POST['addr_start'][$k]);
			$addr_end=check_ip(@$_POST['addr_end'][$k]);
			if($addr_start && $addr_end)
			{
				$sql='UPDATE '.TRACKER_ZONES_ADDR." SET addr_start=INET_ATON('{$addr_start}'), addr_end=INET_ATON('{$addr_end}') WHERE id='".my_int_val($k)."'";
				$result=$db->sql_query($sql);
			}
		}
	}
	if($d_addr)
	{
		$sql='DELETE FROM '.TRACKER_ZONES_ADDR." WHERE id IN('".implode("', '", $d_addr)."')";
		$result=$db->sql_query($sql);
	}
	$this->u_action=append_sid("{$phpbb_admin_path}index.$phpEx", 'i=board&amp;mode=tzones&amp;view_addr=1&amp;tzone='.$tzone);
}
if(request_var('add_rtrack', 0) && ($tzone=request_var('tzone', 0)))
{
	$d_rtrack=$exs_rtrack=array();
	foreach(@$_POST['rtrack_url'] as $k=>$v)
	{
		if(@$_POST['rtrack_delete'][$k])
		{
			$d_rtrack[]=my_int_val($k);
		}
		else
		{
			if(@$_POST['rtrack_url'][$k]!='' && (!preg_match('#^(http|udp):\/\/(\w+|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})#', @$_POST['rtrack_url'][$k]) || strlen(@$_POST['rtrack_url'][$k]) > 512) || in_array(@$_POST['rtrack_url'][$k], $exs_rtrack))
			{
				trigger_error($user->lang['INVALID_RTRACK_URL']);
				continue;
			}

			$_POST['rtrack_remote'][$k]=!$_POST['rtrack_remote'][$k] ? 0 : ($_POST['rtrack_remote'][$k]==1 ? 1 : -1);
			if($k==0 && @$_POST['rtrack_url'][$k]!='')
			{
				$exs_rtrack[]=@$_POST['rtrack_url'][$k];
				$sql='INSERT INTO '.TRACKER_RTRACK_TABLE."
					(zone_id, rtrack_url, rtrack_remote)
						VALUES('".(!$_POST['rtrack_remote'][$k] ? $tzone : 0)."',
						'".$db->sql_escape($_POST['rtrack_url'][$k])."',
						'".$db->sql_escape($_POST['rtrack_remote'][$k])."')";
				$result=$db->sql_query($sql);
			}
			else if(@$_POST['rtrack_url'][$k]!='')
			{
				$exs_rtrack[]=@$_POST['rtrack_url'][$k];
				$sql='UPDATE '.TRACKER_RTRACK_TABLE." SET
					zone_id='".(!$_POST['rtrack_remote'][$k] ? $tzone : 0)."',
					rtrack_url='".$db->sql_escape($_POST['rtrack_url'][$k])."',
					rtrack_remote='".$db->sql_escape($_POST['rtrack_remote'][$k])."'
					 WHERE id='".my_int_val($k)."'";
				$result=$db->sql_query($sql);
			}
		}
	}
	if($d_rtrack)
	{
		$sql='DELETE FROM '.TRACKER_RTRACK_TABLE." WHERE id IN('".implode("', '", $d_rtrack)."')";
		$result=$db->sql_query($sql);
	}
	$this->u_action=append_sid("{$phpbb_admin_path}index.$phpEx", 'i=board&amp;mode=tzones&amp;view_rtrack=1&amp;tzone='.$tzone);
}

if(request_var('view_addr', 0))
{
	$tzone=request_var('tzone', 0);
	$zones_title='ACP_TRACKER_ADDR';
	if($tzone)
	{
		$sql='SELECT zone_name, zone_type FROM '.TRACKER_ZONES_NAME." WHERE id='{$tzone}'";
		$result=$db->sql_query($sql);
		$zone_name=$db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		$sql='SELECT
			id, INET_NTOA(addr_start) addr_start, INET_NTOA(addr_end) addr_end
				FROM '.TRACKER_ZONES_ADDR."
					WHERE zone_id='{$tzone}'";
		$result=$db->sql_query($sql);
		while($row=$db->sql_fetchrow($result))
		{
			$template->assign_block_vars('addrs', array(
				'COUNT'	=> $row['id'],
				'START'	=> $row['addr_start'],
				'END'	=> $row['addr_end'],
				)
			);
		}
		$db->sql_freeresult($result);
		$template->assign_block_vars('addrs', array(
				'COUNT'	=> 0,
				'START'	=> '',
				'END'	=> '',
				)
			);

		if($zone_name['zone_type']=='w')
		{
			$l_zone_type=$user->lang['ZONE_WHITE'];
		}
		else if($zone_name['zone_type']=='b')
		{
			$l_zone_type=$user->lang['ZONE_BLACK'];
		}
		else if($zone_name['zone_type']=='r')
		{
			$l_zone_type=$user->lang['ZONE_BLOCK'];
		}
		else
		{
			$l_zone_type='';
		}
		$template->assign_vars(array(
			'ZONE_NAME' => htmlspecialchars($zone_name['zone_name']),
			'ZONE_TYPE' => $l_zone_type,
			'S_HIDDEN_FIELDS'=>'<input type="hidden" name="add_addr" value="1" />
						<input type="hidden" name="tzone" value="'.$tzone.'" />',
			)
		);
	}
	$template->assign_vars(array(
		'S_SET_ADDR'	=> true,
		)
	);
}
else if(request_var('view_rtrack', 0))
{
	$tzone=request_var('tzone', 0);
	$zones_title='ACP_TRACKER_RTRACK';
	if($tzone)
	{
		$sql='SELECT zone_name, zone_type FROM '.TRACKER_ZONES_NAME." WHERE id='{$tzone}'";
		$result=$db->sql_query($sql);
		$zone_name=$db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		$sql='SELECT rt.*, SUM(ra.seeders) seeders, SUM(ra.leechers) leechers, SUM(ra.seeders)+SUM(ra.leechers) peers, SUM(ra.times_completed) completed, SUM(ra.err_count) errors, SUM(1) torrents FROM '.TRACKER_RTRACK_TABLE." rt LEFT JOIN ".TRACKER_RANNOUNCES_TABLE." ra ON (rt.id=ra.tracker) WHERE rt.zone_id='{$tzone}' AND rt.torrent='0' AND rt.rtrack_user='0' GROUP BY rt.id ORDER BY rt.zone_id";
		$result=$db->sql_query($sql);
		while($row=$db->sql_fetchrow($result))
		{
			$template->assign_block_vars('rtracks', array(
				'COUNT'	=> $row['id'],
				'URL'	=> htmlspecialchars($row['rtrack_url']),
				'TYPE'=>'
					<select name="rtrack_remote['.$row['id'].']">
						<option value="0"></option>
						<option value="-1"'.($row['rtrack_remote']==-1 && !$row['zone_id'] ? ' selected="selected"' : '').'>'.$user->lang['RTRACKER_UNITED'].'</option>
						<option value="1"'.($row['rtrack_remote']==1 && !$row['zone_id'] ? ' selected="selected"' : '').'>'.$user->lang['RTRACKER_REMOTE_UNITED'].'</option>
					</select>
					',
				'STAT'=>$row['rtrack_remote']==1 ? sprintf($user->lang['RTRACKER_STAT'], $row['seeders'], $row['leechers'], $row['peers'], $row['completed'], $row['torrents'], $row['errors'], ($row['torrents'] ? $row['errors']/$row['torrents'] : 0)) : '',
				)
			);
		}
		$db->sql_freeresult($result);
		$template->assign_block_vars('rtracks', array(
				'COUNT'	=> 0,
				'URL'	=> '',
				'TYPE'=>'
<select name="rtrack_remote[0]">
	<option value="0"></option>
	<option value="-1">'.$user->lang['RTRACKER_UNITED'].'</option>
	<option value="1">'.$user->lang['RTRACKER_REMOTE_UNITED'].'</option>
</select>
		',
				)
			);
		if($zone_name['zone_type']=='w')
		{
			$l_zone_type=$user->lang['ZONE_WHITE'];
		}
		else if($zone_name['zone_type']=='b')
		{
			$l_zone_type=$user->lang['ZONE_BLACK'];
		}
		else if($zone_name['zone_type']=='r')
		{
			$l_zone_type=$user->lang['ZONE_BLOCK'];
		}
		else
		{
			$l_zone_type='';
		}
		$template->assign_vars(array(
			'ZONE_NAME' => htmlspecialchars($zone_name['zone_name']),
			'ZONE_TYPE' => $l_zone_type,
			'S_HIDDEN_FIELDS'=>'<input type="hidden" name="add_rtrack" value="1" />
						<input type="hidden" name="tzone" value="'.$tzone.'" />',
			)
		);
	}
	$template->assign_vars(array(
		'S_SET_RTRACK'	=> true,
		)
	);
}
else
{
	$sql='SELECT * FROM '.TRACKER_ZONES_NAME.' ORDER BY zone_type';
	$result=$db->sql_query($sql);
	while($row=$db->sql_fetchrow($result))
	{
		$template->assign_block_vars('zones', array(
			'COUNT'	=> $row['id'],
			'NAME'	=> htmlspecialchars($row['zone_name']),
			'A_VIEW_ADDR'	=> append_sid("{$phpbb_admin_path}index.$phpEx", 'i=board&amp;mode=tzones&amp;view_addr=1&amp;tzone='.$row['id']),
			'A_VIEW_RTRACK' => append_sid("{$phpbb_admin_path}index.$phpEx", 'i=board&amp;mode=tzones&amp;view_rtrack=1&amp;tzone='.$row['id']),
			'TYPE'=>'
<select name="z_type['.$row['id'].']">
	<option value="a"></option>
	<option value="w"'.($row['zone_type']=='w' ? ' selected="selected"' : '').'>'.$user->lang['ZONE_WHITE'].'</option>
	<option value="b"'.($row['zone_type']=='b' ? ' selected="selected"' : '').'>'.$user->lang['ZONE_BLACK'].'</option>
	<option value="r"'.($row['zone_type']=='r' ? ' selected="selected"' : '').'>'.$user->lang['ZONE_BLOCK'].'</option>
</select>
			',
			)
		);
	}
	$db->sql_freeresult($result);
	$template->assign_block_vars('zones', array(
			'COUNT'	=> 0,
			'NAME'	=> '',
			)
		);

	$template->assign_vars(array(
		'S_VIEW_ZONES'	=> true,
		'S_HIDDEN_FIELDS'=>'<input type="hidden" name="add_zones" value="1" />',
		)
	);
}

$display_vars = array(
	'title'	=> $zones_title,
	'vars'	=> array(
		'legend1'				=> 'ACP_TRACKER_ZONES_SETTINGS',
		)
	);
$template->assign_vars(array(
	'S_ZONES_INC'	=> true,
	)
);
?>
